package com.danco.training.zaneuskaya.connection.dao.impl;


import java.util.Calendar;
import java.util.Date;
import java.util.List;














import org.apache.log4j.Logger;


import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.danco.training.zaneuskaya.connection.dao.AbstractHibernateDao;
import com.danco.training.zaneuskaya.connection.dao.BookDao;
import com.danco.training.zaneuskaya.domain.Book;


public class BookDaoImpl extends AbstractHibernateDao<Book, Integer> implements BookDao{
	
		private static final Logger logger = Logger
			.getLogger(BookDaoImpl.class);
	
	
	
	@SuppressWarnings("unchecked")
	public List<Book> readSomeMonthsUnsoldBooks(Session session, int months) {
		
			Criteria criteria = session.createCriteria(Book.class);
			Date date = new Date();
			Calendar c1 = Calendar.getInstance(); 
			c1.setTime(date);
			c1.add(Calendar.MONTH, - months);
			criteria.add(Restrictions.lt("receiptDate", c1.getTime()));
			
		return criteria.list();
		
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Book> readBooksSortedByAlphabet(Session session) {
		Criteria criteria = session.createCriteria(Book.class);
		criteria.addOrder(Order.asc("title"));
		return criteria.list();
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Book> readBooksSortedByPublicationDate(Session session) {
		Criteria criteria = session.createCriteria(Book.class);
		criteria.addOrder(Order.asc("publicationDate"));
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Book> readBooksSortedByPrice(Session session) {
		Criteria criteria = session.createCriteria(Book.class);
		criteria.addOrder(Order.asc("price"));
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Book> readBooksSortedByAvailability(Session session) {
		Criteria criteria = session.createCriteria(Book.class);
		criteria.addOrder(Order.asc("available"));
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Book> readUnsoldBooksSortedByReceiptDate(Session session, int months) {
		Criteria criteria = session.createCriteria(Book.class);
		Date date = new Date();
		Calendar c1 = Calendar.getInstance(); 
		c1.setTime(date);
		c1.add(Calendar.MONTH, - months);
		criteria.add(Restrictions.lt("receiptDate", c1.getTime()));
		criteria.addOrder(Order.asc("receiptDate"));
	return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Book> readUnsoldBooksSortedByPrice(Session session, int months) {
		Criteria criteria = session.createCriteria(Book.class);
		Date date = new Date();
		Calendar c1 = Calendar.getInstance(); 
		c1.setTime(date);
		c1.add(Calendar.MONTH, - months);
		criteria.add(Restrictions.lt("receiptDate", c1.getTime()));
		criteria.addOrder(Order.asc("price"));
	return criteria.list();
	}

	
	@SuppressWarnings("unchecked")
	public List<Book> readAvailableBooks(Session session) {
		Criteria criteria = session.createCriteria(Book.class);
		criteria.add(Restrictions.eq("available", true));
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Book> readNotAvailableBooks(Session session) {
		Criteria criteria = session.createCriteria(Book.class);
		criteria.add(Restrictions.eq("available", false));
		return criteria.list();
	}

	@Override
	public Book read(Session session, Integer PK)
			throws HibernateException {
		
		return (Book) super.read(session, Book.class, PK);
	}

	@Override
	public List<Book> readAll(Session session) throws HibernateException {
		
		return super.readAll(Book.class, session);
	}

	
	
	
}
